<?php

/**
 * Class TaxonomyViewAction
 */
class TaxonomyViewAction extends CAction
{
	public $view = 'view';

	public $bundle;

	public function run($id)
	{
		$weights = isset($_POST['weight']) ? $_POST['weight'] : null;
		if (isset($_POST['saveWeight']) && $weights) {
			foreach ((array)$weights as $termId => $weight) {
				Term::model()->updateWeightByPk($termId, $weight);
			}
			Yii::app()->getCache()->delete("term_tree_{$id}");
			Yii::app()->getUser()->setFlash('success', '保存权重成功');
			$this->getController()->redirect(array('view', 'id' => $id));
		}
		$this->getController()->render($this->view, array('model' => $this->loadModel($id)));
	}

	public function loadModel($id)
	{
		if ($this->bundle) {
			$condition = 'bundle=:bundle';
			$params = array(':bundle' => $this->bundle);
		} else {
			$condition = '';
			$params = array();
		}
		if (!$model = Taxonomy::model()->findByPk($id, $condition, $params)) {
			throw new CHttpException(404, '页面没有找到');
		}
		return $model;
	}
} 